﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Media.Imaging;

namespace AutoTrader
{
    public partial class Page : UserControl
    {

        public Page()
        {
            // initialize the UI components
            InitializeComponent();

            // iterate through the list of cars and add each item
            // to the list as an image. The Tag property of the
            // image is a handy storage mechanism for holding the 
            // item id. That will be used later to retrieve the details 
            // from the collection.
            this.lstCars.Items.Clear();
            foreach (CarListing cl in GetCarList())
            {
                BitmapImage bmpImg = new BitmapImage();
                Uri uri = new Uri(cl.imageSource, UriKind.Relative);
                bmpImg.UriSource = uri;
                Image img = new Image();
                img.Source = bmpImg;
                img.Tag = cl.imageSource;
                //img.Height = 270;
                img.Width = 320;
                img.MouseLeftButtonDown += this.ListBoxItem_MouseLeftButtonDown;
                lstCars.Items.Add(img);
            }
        }
        
        /// <summary>
        /// This function returns a hard-coded list of car details. 
        /// </summary>
        /// <returns></returns>
        private List<CarListing> GetCarList()
        {
            List<CarListing> ListOCars = new List<CarListing>();

            CarListing cl = new CarListing();
            cl.imageSource = "mustang1.jpg";
            cl.Make = "Ford";
            cl.Model = "Mustang GT 500";
            cl.Year = "2009";
            cl.Price = "$54,000.00";
            ListOCars.Add(cl);

            CarListing cl2 = new CarListing();
            cl2.imageSource = "mustang2.jpg";
            cl2.Make = "Ford";
            cl2.Model = "Mustang Cobra";
            cl2.Year = "2008";
            cl2.Price = "$45,000.00";
            ListOCars.Add(cl2);

            CarListing cl3 = new CarListing();
            cl3.imageSource = "mustang3.jpg";
            cl3.Make = "Ford";
            cl3.Model = "Mustang Sedan";
            cl3.Year = "2005";
            cl3.Price = "$32,500.00";

            ListOCars.Add(cl3);

            return ListOCars;
        }

        private void ListBoxItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.FadeOut.Begin();

            BitmapImage bmpImg = new BitmapImage();
            Image img = (Image)sender;
            Uri uri2 = new Uri(img.Tag.ToString(), UriKind.Relative);
            bmpImg.UriSource = uri2;

            this.imgSelectedCar.Source = bmpImg;

            this.FadeIn.Begin();

            // This is an example of a LINQ query. A collection may be queried using
            // a SQL type of syntax. If LINQ to SQL were being used, the syntax would
            // be identical.
            CarListing car = GetCarList().Where(cl => cl.imageSource == img.Tag.ToString()).SingleOrDefault();
            if (car != null)
            {
                this.txtMake.Text = car.Make;
                this.txtModel.Text = car.Model;
                this.txtPrice.Text = car.Price;
                this.txtYear.Text = car.Year;
            }
        }
    }

    /// <summary>
    /// This class provides a data structure for holding car details.
    /// </summary>
    public class CarListing
    {
        public string imageSource;
        public string Make;
        public string Model;
        public string Year;
        public string Price;
    }
}
